﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OsuKeyboard
{
    class HidKeyCode
    {
        /// <summary>
        /// 通过字典值查找对应的键
        /// </summary>
        /// <param name="dic">字典名</param>
        /// <param name="value">字典值</param>
        /// <returns>返回对应值的键</returns>
        public static string FindKeyFromCodes(Dictionary<string, byte> dic, byte value)
        {
            string result = "";
            if (dic.ContainsValue(value))
            {
                foreach (var item in dic)
                {
                    if (item.Value == value)
                    {
                        result = item.Key;
                        break;
                    }
                    
                }
            }
            return result;
        }
        /// <summary>
        /// 转换组合键值为对应的名称
        /// </summary>
        /// <returns>返回组合键名称字符串，以'+'号连接</returns>
        public static string ConverComcode(byte value, int DeviceVer)
        {
			StringBuilder ComStr = new StringBuilder();
            if (DeviceVer == 1) return "";	// 旧版本键盘不支持组合键
			if ((value & 0x01) != 0) ComStr.Append("LCtrl + ");
			if ((value & 0x02) != 0) ComStr.Append("LShift + ");
			if ((value & 0x04) != 0) ComStr.Append("LAlt + ");
			if ((value & 0x08) != 0) ComStr.Append("LWin + ");
			if ((value & 0x10) != 0) ComStr.Append("RCtrl + ");
			if ((value & 0x20) != 0) ComStr.Append("RShift + ");
			if ((value & 0x40) != 0) ComStr.Append("RAlt + ");
			if ((value & 0x80) != 0) ComStr.Append("RWin + ");
			return ComStr.ToString();
        }
        //HID键值对照表
        public static Dictionary<string, byte> HidKeypadCode = new Dictionary<string, byte>
        {
            {"a",0x04},         //A
            {"b",0x05},         //B
            {"c",0x06},         //C
            {"d",0x07},         //D
            {"e",0x08},         //E
            {"f",0x09},         //F
            {"g",0x0A},         //G
            {"h",0x0B},         //H
            {"i",0x0C},         //I
            {"j",0x0D},         //J
            {"k",0x0E},         //K
            {"l",0x0F},         //L
            {"m",0x10},         //M
            {"n",0x11},         //N
            {"o",0x12},         //O
            {"p",0x13},         //P
            {"q",0x14},         //Q
            {"r",0x15},         //R
            {"s",0x16},         //S
            {"t",0x17},         //T
            {"u",0x18},         //U
            {"v",0x19},         //V
            {"w",0x1A},         //W
            {"x",0x1B},         //X
            {"y",0x1C},         //Y
            {"z",0x1D},         //Z
            {"1",0x1E},         //D1                数字 1
            {"2",0x1F},         //D2                数字 2
            {"3",0x20},         //D3                数字 3
            {"4",0x21},         //D4                数字 4
            {"5",0x22},         //D5                数字 5
            {"6",0x23},         //D6                数字 6
            {"7",0x24},         //D7                数字 7
            {"8",0x25},         //D8                数字 8
            {"9",0x26},         //D9                数字 9
            {"0",0x27},         //D0                数字 0
            {"Enter",0x28},     //Return            回车键
            {"Esc",0x29},       //Escape
            {"Back",0x2A},      //Back              退格键
            {"Tab",0x2B},       //Tab
            {"Space",0x2C},     //Space             空格键
            {"-",0x2D},         //OemMinus          -_
            {"=",0x2E},         //Oemplus           =+
            {"[",0x2F},         //OemOpenBrackets   [{
            {"]",0x30},         //Oem6              ]}
            {@"\",0x31},        //Oem5              \|
            {";",0x33},         //Oem1              ;:
            {"'",0x34},         //Oem7              '
            {"`",0x35},         //Oemtilde          `~
            {",",0x36},         //Oemcomma          ,<
            {".",0x37},         //OemPeriod         .>
            {"/",0x38},         //OemQuestion       /?
            {"CapsLock",0x39},  //Capital           CapsLock
            {"F1",0x3A},        //F1
            {"F2",0x3B},        //F2
            {"F3",0x3C},        //F3
            {"F4",0x3D},        //F4
            {"F5",0x3E},        //F5
            {"F6",0x3F},        //F6
            {"F7",0x40},        //F7
            {"F8",0x41},        //F8
            {"F9",0x42},        //F9
            {"F10",0x43},       //F10
            {"F11",0x44},       //F11
            {"F12",0x45},       //F12
            {"PrtScn",0x46},    //Print Screen
            {"ScrLk",0x47},     //Scroll            ScrollLock
            {"Pause",0x48},     //Pause             PauseBreak
            {"Ins",0x49},       //Insert
            {"Home",0x4A},      //Home
            {"PgUp",0x4B},      //PageUp
            {"Del",0x4C},       //Delete
            {"End",0x4D},       //End
            {"PgDn",0x4E},      //Next              PageDown
            {"→",0x4F},        //Right             右方向键
            {"←",0x50},        //Left              左方向键
            {"↓",0x51},        //Down              下方向键
            {"↑",0x52},        //Up                上方向键
            {"NumLock",0x53},   //NumLock           NumLock
            {"Num(/)",0x54},    //Divide            小键盘 /
            {"Num(*)",0x55},    //Multiply          小键盘 *
            {"Num(-)",0x56},    //Subtract          小键盘 -
            {"Num(+)",0x57},   //Add               小键盘 +
            {"Num(1)",0x59},    //NumPad1           小键盘 1
            {"Num(2)",0x5A},    //NumPad2           小键盘 2
            {"Num(3)",0x5B},    //NumPad3           小键盘 3
            {"Num(4)",0x5C},    //NumPad4           小键盘 4
            {"Num(5)",0x5D},    //NumPad5           小键盘 5
            {"Num(6)",0x5E},    //NumPad6           小键盘 6
            {"Num(7)",0x5F},    //NumPad7           小键盘 7
            {"Num(8)",0x60},    //NumPad8           小键盘 8
            {"Num(9)",0x61},    //NumPad9           小键盘 9
            {"Num(0)",0x62},    //NumPad0           小键盘 0
            {"Num(.)",0x63},    //Decimal           小键盘 .
            {"↲",0x58},         //Enter             小键盘 Enter
            {"Apps",0x65},      //Apps              鼠标右键快捷键
            {"WakeUp",0x03},    //WakeUp            唤醒键(暂未用)
            {"Sleep",0x03},     //Sleep             休眠键(暂未用)
            {"Power",0x03},     //Power             电源键(暂未用)
            {"None",0x00}       //NoEvent           无键值
        };
    }
}
